package groupId.task;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

public class TaskInputDeserializer extends JsonDeserializer<TaskInput> {
    @Override
    public TaskInput deserialize(JsonParser jsonParser, DeserializationContext ctx)
            throws IOException {
        ObjectCodec oc = jsonParser.getCodec();
        JsonNode node = oc.readTree(jsonParser);
        TaskInput taskInput = null;
        switch (node.get("type").asText()) {
            case "sms":
                taskInput = new TaskSmsInput();
                break;
            case "img":
                taskInput = new TaskOcrInput();
                break;
            case "idpPass":
                taskInput = new TaskIdpInput();
                break;
        }
        if (taskInput != null && node.get("value") != null) {
            taskInput.setValue(node.get("value").asText());
        }
        return taskInput;
    }
}